<template>
    <div>
        <Alert>
            <Row type="flex" justify="start" :gutter="20">
                <!--<Col span="5">
                    <Input @on-enter="search" clearable placeholder="请输入单位编码" v-model="searchObj.companyCode"
                           icon="ios-search"/>
                </Col>-->
                <Col span="5">
                    <Input @on-enter="search" clearable placeholder="请输入单位名称" v-model="searchObj.companyName"
                           icon="ios-search"/>
                </Col>
                <Col span="3">
                    <Button class="search-btn" type="primary" @click="addBtn">
                        <Icon type="md-add"/>
                        添加
                    </Button>
                </Col>
            </Row>
        </Alert>
        <br>
        <Row type="flex" justify="center">
            <Col span="24">
                <Table border :loading="tableLoading" :columns="tableColumns" :data="tableData">
                    <template slot="state" slot-scope="{ row }">
                        <span v-if="row.state === '0'">待审核</span>
                        <span class="examine-t" v-else>审核通过</span>
                    </template>
                    <template slot="action" slot-scope="{ row }">
                        <Button type="primary" size="small" class="action-btn" @click="examineBtn(row.id)">审核</Button>
                        <Button type="primary" size="small" class="action-btn" @click="editBtn(row)">编辑</Button>
                        <Button type="info" size="small" class="action-btn" @click="infoBtn(row)">详情</Button>
                        <Button type="error" size="small" class="action-btn" @click="delBtn(row.id)">删除</Button>
                    </template>
                </Table>
            </Col>
        </Row>
        <br>
        <Row type="flex" justify="end">
            <Page show-total :current="pageObj.pageNo" :page-size="pageObj.pageSize" :total="pageObj.totalRecord"
                  @on-change="getPageNo"/>
        </Row>
        <option-modal ref="optionModal" :modal-title="modalTitle" :modal-show="modalShow" :modal-type="modalType"
                      :dataObj="modalData" @cancelOption="cancelOption" @saveOption="saveOption"></option-modal>
    </div>
</template>

<script>
import {
  getJZLLTCList,
  insertJZLLTCInfo
} from '@/api/yi-qing'
import OptionModal from './components/option-modal'

export default {
  components: { OptionModal },
  data () {
    return {
      spinShow: false,
      searchObj: {},
      modalShow: false,
      modalTitle: '',
      modalType: '',
      modalData: {},
      tableColumns: [
        {
          type: 'index',
          title: '序号',
          width: 70,
          fixed: 'left',
          align: 'center'
        },
        // {
        //   title: '单位编码',
        //   key: 'company_code',
        //   width: 200,
        //   align: 'center'
        // },
        {
          title: '单位名称',
          key: 'company_name',
          width: 200,
          fixed: 'left',
          align: 'center'
        },
        {
          title: '审核状态',
          slot: 'state',
          width: 130,
          fixed: 'left',
          align: 'center'
        },
        {
          title: '日期',
          width: 150,
          fixed: 'left',
          key: 'date',
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('span', this.$common.xeUtils.toDateString(params.row.date, 'yyyy - MM - dd'))
            ])
          }
        },
        {
          title: '投入医护人员总数',
          width: 200,
          key: 'tryhryzs',
          align: 'center'
        },
        {
          title: '投入医护人员院感',
          width: 200,
          key: 'tryhryyg',
          align: 'center'
        },
        {
          title: '负压病房数',
          width: 200,
          key: 'fybfs',
          align: 'center'
        },
        {
          title: '普通病房数',
          width: 200,
          key: 'ptbfs',
          align: 'center'
        },
        {
          title: '床位数',
          width: 200,
          key: 'cws',
          align: 'center'
        },
        {
          title: '已使用床位数',
          width: 200,
          key: 'ysycws',
          align: 'center'
        },
        {
          title: '操作',
          minWidth: 250,
          fixed: 'right',
          slot: 'action',
          align: 'center'
        }
      ],
      tableData: [],
      selectData: [],
      tableLoading: false,
      pageObj: {
        pageNo: 1,
        pageSize: 10,
        totalRecord: 0
      }
    }
  },
  methods: {
    cancelOption () {
      this.modalShow = false
    },
    saveOption () {
      this.modalShow = false
      this.init()
    },
    success (val) {
      val = this.$common.xeUtils.isEmpty(val) ? '操作成功' : val
      this.$Message.success(val)
    },
    error (data) {
      this.$Message.error({
        content: '操作失败,\n' + data.errmsg
      })
    },
    getPageNo () {
      this.pageObj.pageNo = pageNo
      this.init(data)
    },
    tableSelectChange (val) {
      this.selectData = val
    },
    initPage () {
      this.pageObj.pageNo = 1
      this.pageObj.pageSize = 10
    },
    init (data) {
      this.tableLoading = true
      getJZLLTCList(data).then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.tableLoading = false
          this.tableData = data.data.results
          this.pageObj.totalRecord = data.data.totalRecord
        } else {
          this.tableLoading = false
          this.error(data)
        }
      })
    },
    search () {
      this.initPage()
      const param = {
        pageNo: this.pageObj.pageNo,
        pageSize: this.pageObj.pageSize,
        company_name: this.$common.xeUtils.trim(this.searchObj.companyName)
      }
      this.init(param)
    },
    addBtn () {
      this.modalTitle = '添加'
      this.modalType = 'add'
      this.modalData = {}
      this.modalShow = true
    },
    editBtn (val) {
      this.modalTitle = '编辑'
      this.modalType = 'edit'
      this.modalData = this.$common.xeUtils.clone(val)
      this.modalShow = true
    },
    infoBtn (val) {
      this.modalTitle = '详情'
      this.modalType = 'info'
      this.modalData = this.$common.xeUtils.clone(val)
      this.modalShow = true
    },
    delBtn (val) {
      this.$Modal.confirm({
        title: '确认删除此条数据么？',
        content: '请仔细核对后再操作',
        loading: true,
        onOk: () => {
          this.option({ id: val, del_flag: 1 })
        }
      })
    },
    examineBtn (val) {
      this.$Modal.confirm({
        title: '确认审核通过么？',
        content: '请仔细核对后再操作',
        loading: true,
        onOk: () => {
          this.option({ id: val, state: 1 })
        }
      })
    },
    option (val) {
      insertJZLLTCInfo(val).then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.$Modal.remove()
          this.init()
          this.success('操作成功')
        } else {
          this.$Modal.remove()
          this.error(data)
        }
      })
    }
  },
  mounted () {
    const param = {
      pageNo: this.pageObj.pageNo,
      pageSize: this.pageObj.pageSize
    }
    this.init(param)
  }
}
</script>

<style scoped>
    .action-btn {
        margin-right: 5px;
        margin-bottom: 5px;
    }

    .search-btn {
        width: 100px;
    }

    .examine-t {
        color: green;
    }
</style>
